// pages/addCar/brandModelChooice.js
var wxaSortPicker = require('../../utils/wxaSortPicker/wxaSortPicker.js');
import config from '../../config.js';
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brandList: [], //品牌
    page: {
      start: 0,
      size: 20,
      refreshTime: '', // 刷新的时间 
      hideHeader: true,
      hideBottom: true,
      loadMoreData: '加载更多……'
    },
    brandId: undefined, //车型id,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.loadData();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //加载品牌列表
  loadData: function() {
    wx.showLoading({
      title: '数据获取中...',
    })
    var that = this;
    wx.request({
      url: base.carBrandList,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: 0,
        length: 20
      },
      success: function(res) {
        wx.hideLoading();
        if (res.statusCode == 200) {
          console.log(res);
          that.setData({
            brandList: res.data.data,
          })
          var array = []
          var map = {};
          for (var i = 0; i < res.data.data.length; i++) {
            var item = res.data.data[i];
            map = {};
            map['name'] = item.name;
            map['value'] = item.modelId;
            map['icon'] = item.logo;

            array.push(map);
          }
          wxaSortPicker.init(array, that);
        } else {
          wx.showToast({
            title: '列表获取失败',
            icon: "none"
          })
        }
      }
    })
  },
  selectBrand: function(e) {
    var modelId = e.currentTarget.dataset.id;
    if (modelId != null) {
      wx.navigateTo({
        url: './brandChooice?modelId=' + modelId,
      })
    }
  },
  //选中返回值
  wxaSortPickerItemTap: function(e) {
    var modelId = e.currentTarget.dataset.value;
    if (modelId != null) {
      wx.navigateTo({
        url: './brandChooice?modelId=' + modelId,
      })
    }
  }
})